local skills = {
    woodie_curse_weremeter_1 = true,
    woodie_curse_weremeter_2 = true,
    woodie_curse_weremeter_3 = true,

    woodie_curse_master = true,

    woodie_curse_beaver_1 = true,
    woodie_curse_beaver_2 = true,
    woodie_curse_beaver_3 = true,

    woodie_curse_epic_beaver = true,

    woodie_curse_moose_1 = true,
    woodie_curse_moose_2 = true,
    woodie_curse_moose_3 = true,

    woodie_curse_epic_moose = true,

    woodie_curse_goose_1 = true,
    woodie_curse_goose_2 = true,
    woodie_curse_goose_3 = true,

    woodie_curse_epic_goose = true,

    woodie_human_lucy_1 = true,
    woodie_human_lucy_2 = true,
    woodie_human_lucy_3 = true,

    woodie_human_quickpicker_1 = true,
    woodie_human_quickpicker_2 = true,
    woodie_human_quickpicker_3 = true,

    woodie_human_treeguard_1 = true,
    woodie_human_treeguard_2 = true,
    woodie_human_treeguard_max = true,

    woodie_allegiance_shadow = true,
    woodie_allegiance_lunar = true,

}

local function _d_resetskilltree(player)

    if not (player and TheWorld.ismastersim) then
        return
    end

    local skilltreeupdater = player.components.skilltreeupdater
    local skilldefs = require("prefabs/skilltree_defs").SKILLTREE_DEFS[player.prefab]
    if skilldefs ~= nil then
        local attempts = 50
        while attempts > 0 do -- FIXME(JBK): This needs to be done in skilltreeupdater and carefully handled for server and client sync.
            local keepgoing = false
            for skill, data in pairs(skilldefs) do
                if data.rpc_id then
                    if skilltreeupdater:IsActivated(skill) then
                        keepgoing = true
                        skilltreeupdater:DeactivateSkill(skill)
                    end
                end
            end
            if not keepgoing then
                break
            end
            attempts = attempts - 1
        end
    end

    skilltreeupdater:AddSkillXP(9999999)
end

local db = TUNING.MOD_MING_WOODIE_SKILLTREE.SKILL_TREE
---@class components
---@field woodie_skill_tree_newunlock_data component_woodie_skill_tree_newunlock_data

-- local function on_val(self, value)
    -- self.inst.replica.woodie_skill_tree_newunlock_data:SetVal(value)
-- end

---@class component_woodie_skill_tree_newunlock_data
---@field inst ent
---@field henshin_times integer # 变身计时1：变身次数
---@field ate_monsterlasagna boolean # 吃过千层饼
---@field workfinish_rock integer # 海狸形态下，采矿单位
---@field workfinish_tree integer # 海狸形态下，砍树单位
---@field workfinish_marble integer # 海狸形态下，开采大理石单位
---@field woodie_curse_epic_beaver boolean # 海狸人精通：吴迪参与击杀熊
---@field henshin_times_moose integer # 变身?次鹿人
---@field woodie_curse_moose_2 boolean # 鹿人2：击杀护士蜘蛛
---@field woodie_curse_moose_3 boolean # 鹿人3: 击杀犀牛类单位（完整犀牛，破碎犀牛，远古犀牛）
---@field woodie_curse_epic_moose boolean # 鹿人精通：吴迪参与击杀梦魇风猪
---@field woodie_curse_goose_1 boolean # 鹅人1：制作鹅人雕像一次
---@field woodie_curse_goose_2 boolean # 鹅人2：击杀海鹦鹉（一种在海边下落的海鸟）
---@field woodie_curse_goose_3 boolean # 鹅人3：击杀金丝雀
---@field woodie_curse_epic_goose boolean # 鹅人精通：吴迪参与击杀驼鹿鹅或邪天翁
---@field woodie_human_lucy_1 boolean # 木工：开局自动解锁
---@field woodie_human_lucy_2 boolean # 帽子雕刻：制作一次任意防具解锁（如果任意防具不好实现可以改成指定猪皮帽）
---@field woodie_human_lucy_3 integer # 拐杖雕刻：使用木工技能雕刻木板三次
---@field woodie_human_quickpicker integer # 快速捡拾者1：完成采集动作40次
---@field woodie_human_treeguard integer # 树精守卫伐木者1：击杀树精单位1次
---@field woodie_allegiance_shadow boolean # 暗影牧人：参与击杀织影者
---@field woodie_allegiance_lunar boolean # 月亮叛徒：参与击杀天体英雄
local woodie_skill_tree_newunlock_data = Class(
---@param self component_woodie_skill_tree_newunlock_data
---@param inst ent
function(self, inst)
    self.inst = inst
    -- self.val = 0

    -- 变身计时1：变身一次
    -- 变身计时2：变身两次
    -- 变色计时3：变身三次
    self.henshin_times = 0

    self.ate_monsterlasagna = false

    self.workfinish_rock = 0
    self.workfinish_tree = 0
    self.workfinish_marble = 0

    self.woodie_curse_epic_beaver = false

    self.henshin_times_moose = 0
    self.woodie_curse_moose_2 = false
    self.woodie_curse_moose_3 = false
    self.woodie_curse_epic_moose = false

    self.woodie_curse_goose_1 = false
    self.woodie_curse_goose_2 = false
    self.woodie_curse_goose_3 = false
    self.woodie_curse_epic_goose = false

    self.woodie_human_lucy_1 = true
    self.woodie_human_lucy_2 = false
    self.woodie_human_lucy_3 = 0

    self.woodie_human_quickpicker = 0

    self.woodie_human_treeguard = 0

    self.woodie_allegiance_shadow = false
    self.woodie_allegiance_lunar = false

    inst:DoTaskInTime(1,function ()
        _d_resetskilltree(self.inst)

        self:_unlock('woodie_human_lucy_1')
    end)

end,
nil,
{
    -- val = on_val,
})

function woodie_skill_tree_newunlock_data:OnSave()
    return {
        -- val = self.val
        henshin_times = self.henshin_times,
        ate_monsterlasagna = self.ate_monsterlasagna,
        workfinish_rock = self.workfinish_rock,
        workfinish_tree = self.workfinish_tree,
        workfinish_marble = self.workfinish_marble,
        woodie_curse_epic_beaver = self.woodie_curse_epic_beaver,
        henshin_times_moose = self.henshin_times_moose,
        woodie_curse_moose_2 = self.woodie_curse_moose_2,
        woodie_curse_moose_3 = self.woodie_curse_moose_3,
        woodie_curse_epic_moose = self.woodie_curse_epic_moose,
        woodie_curse_goose_1 = self.woodie_curse_goose_1,
        woodie_curse_goose_2 = self.woodie_curse_goose_2,
        woodie_curse_goose_3 = self.woodie_curse_goose_3,
        woodie_curse_epic_goose = self.woodie_curse_epic_goose,
        woodie_human_lucy_1 = self.woodie_human_lucy_1,
        woodie_human_lucy_2 = self.woodie_human_lucy_2,
        woodie_human_lucy_3 = self.woodie_human_lucy_3,
        woodie_human_quickpicker = self.woodie_human_quickpicker,
        woodie_human_treeguard = self.woodie_human_treeguard,
        woodie_allegiance_shadow = self.woodie_allegiance_shadow,
        woodie_allegiance_lunar = self.woodie_allegiance_lunar,
    }
end

function woodie_skill_tree_newunlock_data:OnLoad(data)
    -- self.val = data.val or 0
    self.henshin_times = data.henshin_times or 0
    self.ate_monsterlasagna = data.ate_monsterlasagna
    self.workfinish_rock = data.workfinish_rock or 0
    self.workfinish_tree = data.workfinish_tree or 0
    self.workfinish_marble = data.workfinish_marble or 0
    self.woodie_curse_epic_beaver = data.woodie_curse_epic_beaver
    self.henshin_times_moose = data.henshin_times_moose or 0
    self.woodie_curse_moose_2 = data.woodie_curse_moose_2
    self.woodie_curse_moose_3 = data.woodie_curse_moose_3
    self.woodie_curse_epic_moose = data.woodie_curse_epic_moose
    self.woodie_curse_goose_1 = data.woodie_curse_goose_1
    self.woodie_curse_goose_2 = data.woodie_curse_goose_2
    self.woodie_curse_goose_3 = data.woodie_curse_goose_3
    self.woodie_curse_epic_goose = data.woodie_curse_epic_goose
    self.woodie_human_lucy_1 = data.woodie_human_lucy_1
    self.woodie_human_lucy_2 = data.woodie_human_lucy_2
    self.woodie_human_lucy_3 = data.woodie_human_lucy_3 or 0
    self.woodie_human_quickpicker = data.woodie_human_quickpicker or 0
    self.woodie_human_treeguard = data.woodie_human_treeguard or 0
    self.woodie_allegiance_shadow = data.woodie_allegiance_shadow
    self.woodie_allegiance_lunar = data.woodie_allegiance_lunar

    self.inst:DoTaskInTime(1.5,function()
        _d_resetskilltree(self.inst)

        -----------第一列
        if self.henshin_times > 0 then
            for i = 1, self.henshin_times do
                self:_unlock('woodie_curse_weremeter_'..i)
            end
        end

        if self.ate_monsterlasagna then
            self:_unlock('woodie_curse_master')
        end
        -----------第二列
        if self.workfinish_rock >= db.woodie_curse_beaver_1 then
            self:_unlock('woodie_curse_beaver_1')
        end
        if self.workfinish_tree >= db.woodie_curse_beaver_2 then
            self:_unlock('woodie_curse_beaver_2')
        end
        if self.workfinish_marble >= db.woodie_curse_beaver_3 then
            self:_unlock('woodie_curse_beaver_3')
        end

        if self.woodie_curse_epic_beaver then
            self:_unlock('woodie_curse_epic_beaver')
        end
        -----------第三列
        if self.henshin_times_moose >= db.woodie_curse_moose_1 then
            self:_unlock('woodie_curse_moose_1')
        end
        if self.woodie_curse_moose_2 then
            self:_unlock('woodie_curse_moose_2')
        end
        if self.woodie_curse_moose_3 then
            self:_unlock('woodie_curse_moose_3')
        end
        if self.woodie_curse_epic_moose then
            self:_unlock('woodie_curse_epic_moose')
        end
        -------------第四列
        if self.woodie_curse_goose_1 then
            self:_unlock('woodie_curse_goose_1')
        end
        if self.woodie_curse_goose_2 then
            self:_unlock('woodie_curse_goose_2')
        end
        if self.woodie_curse_goose_3 then
            self:_unlock('woodie_curse_goose_3')
        end
        if self.woodie_curse_epic_goose then
            self:_unlock('woodie_curse_epic_goose')
        end
        -----------中间列 下
        -- if self.woodie_human_lucy_1 then
            self:_unlock('woodie_human_lucy_1')
        -- end
        if self.woodie_human_lucy_2 then
            self:_unlock('woodie_human_lucy_2')
        end
        if self.woodie_human_lucy_3 >= db.woodie_human_lucy_3 then
            self:_unlock('woodie_human_lucy_3')
        end
        -----------中间列 左上
        if self.woodie_human_quickpicker >= db.woodie_human_quickpicker_1 then
            self:_unlock('woodie_human_quickpicker_1')
        end
        if self.woodie_human_quickpicker >= db.woodie_human_quickpicker_2 then
            self:_unlock('woodie_human_quickpicker_2')
        end
        if self.woodie_human_quickpicker >= db.woodie_human_quickpicker_3 then
            self:_unlock('woodie_human_quickpicker_3')
        end
        -----------中间列 右上
        if self.woodie_human_treeguard >= db.woodie_human_treeguard_1 then
            self:_unlock('woodie_human_treeguard_1')
        end
        if self.woodie_human_treeguard >= db.woodie_human_treeguard_2 then
            self:_unlock('woodie_human_treeguard_2')
        end
        if self.woodie_human_treeguard >= db.woodie_human_treeguard_3 then
            self:_unlock('woodie_human_treeguard_max')
        end
        ---------
        if self.woodie_allegiance_shadow then
            self:_unlock('woodie_allegiance_shadow')
        end
        if self.woodie_allegiance_lunar then
            self:_unlock('woodie_allegiance_lunar')
        end
    end)


end


---解锁
---@param skillname string
function woodie_skill_tree_newunlock_data:_unlock(skillname)
    if self.inst.components.skilltreeupdater then
        self.inst.components.skilltreeupdater:ActivateSkill(skillname,'woodie',false)
        self.inst.components.skilltreeupdater:AddSkillXP(9999999)
    end
end

---变身次数解锁
---@param weremode 'beaver'|'moose'|'goose'
function woodie_skill_tree_newunlock_data:ForUnlock_Henshin(weremode)
    self.henshin_times = math.min(3,self.henshin_times + 1)
    for i = 1, self.henshin_times do
        self:_unlock('woodie_curse_weremeter_'..i)
    end

    if weremode == 'moose' then -- 变身三次鹿人
        self.henshin_times_moose = math.min(db.woodie_curse_moose_1,self.henshin_times_moose + 1)
        if self.henshin_times_moose >= db.woodie_curse_moose_1 then
            self:_unlock('woodie_curse_moose_1')
        end
    end
end

---吃千层饼解锁
function woodie_skill_tree_newunlock_data:ForUnlock_EatMonsterLasagna()
    self.ate_monsterlasagna = true
    self:_unlock('woodie_curse_master')
end

---work完成解锁
---@param target ent
function woodie_skill_tree_newunlock_data:ForUnlock_WorkFinish(target)
    -- if self.inst.components.wereness and self.inst.components.wereness:GetWereMode() == 'beaver' then
    if self.inst.weremode and self.inst.weremode:value() == 1 then
        if target:HasTag('tree') then
            self.workfinish_tree = math.min(db.woodie_curse_beaver_2,self.workfinish_tree + 1)
            if self.workfinish_tree >= db.woodie_curse_beaver_2 then
                self:_unlock('woodie_curse_beaver_2')
            end
        elseif string.find(target.prefab,'marble') then
            self.workfinish_marble = math.min(db.woodie_curse_beaver_3,self.workfinish_marble + 1)
            if self.workfinish_marble >= db.woodie_curse_beaver_3 then
                self:_unlock('woodie_curse_beaver_3')
            end
        elseif target:HasTag('boulder') then
            self.workfinish_rock = math.min(db.woodie_curse_beaver_1,self.workfinish_rock + 1)
            if self.workfinish_rock >= db.woodie_curse_beaver_1 then
                self:_unlock('woodie_curse_beaver_1')
            end
        end
    end
end

---击杀|联合击杀
---@param victim ent
function woodie_skill_tree_newunlock_data:ForUnlock_KillMob(victim)
    local victim_prefab = victim.prefab
    SUGAR_ming_woodie_skilltree:declare(victim_prefab)
    if victim_prefab == 'leif_sparse' or victim_prefab == 'leif' then
        self.woodie_human_treeguard = math.min(db.woodie_human_treeguard_3,self.woodie_human_treeguard + 1)
        if self.woodie_human_treeguard >= db.woodie_human_treeguard_1 then
            self:_unlock('woodie_human_treeguard_1')
        end
        if self.woodie_human_treeguard >= db.woodie_human_treeguard_2 then
            self:_unlock('woodie_human_treeguard_2')
        end
        if self.woodie_human_treeguard >= db.woodie_human_treeguard_3 then
            self:_unlock('woodie_human_treeguard_max')
        end
    end
    if string.find(victim_prefab,"bearger") then
        self.woodie_curse_epic_beaver = true
        self:_unlock('woodie_curse_epic_beaver')
    elseif victim_prefab == 'spider_healer' then
        self.woodie_curse_moose_2 = true
        self:_unlock('woodie_curse_moose_2')
    elseif victim_prefab == "rook" or victim_prefab == "rook_nightmare" or victim_prefab == "minotaur" then
        self.woodie_curse_moose_3 = true
        self:_unlock('woodie_curse_moose_3')
    elseif victim_prefab == "daywalker" then
        self.woodie_curse_epic_moose = true
        self:_unlock('woodie_curse_epic_moose')
    elseif victim_prefab == "puffin" then
        self.woodie_curse_goose_2 = true
        self:_unlock('woodie_curse_goose_2')
    elseif victim_prefab == "canary" then
        self.woodie_curse_goose_3 = true
        self:_unlock('woodie_curse_goose_3')
    elseif (victim_prefab == "moose1" or victim_prefab == "moose2") or victim_prefab == "malbatross" then
        self.woodie_curse_epic_goose = true
        self:_unlock('woodie_curse_epic_goose')
    elseif string.find(victim_prefab,"stalker") then
        self.woodie_allegiance_shadow = true
        self:_unlock('woodie_allegiance_shadow')
    elseif victim_prefab == 'alterguardian_phase3' then
        self.woodie_allegiance_lunar = true
        self:_unlock('woodie_allegiance_lunar')
    end
end

---制作物品
---@param item ent
---@param the_recipe table
function woodie_skill_tree_newunlock_data:ForUnlock_BuildItem(item,the_recipe)

    local item_prefab = item.prefab
    if item_prefab == 'wereitem_goose' then
        self.woodie_curse_goose_1 = true
        self:_unlock('woodie_curse_goose_1')
    elseif (item.components.equippable and item.components.equippable.equipslot ~= EQUIPSLOTS.HANDS) and item.components.armor then
        self.woodie_human_lucy_2 = true
        self:_unlock('woodie_human_lucy_2')
    elseif item_prefab == 'boards' then
        local has_lucy,has_log = false,false
        for _,v in pairs(the_recipe.ingredients) do
            for _,vv in pairs(v) do
                if vv == 'lucy' then
                    has_lucy = true
                end
                if vv == 'log' then
                    has_log = true
                end
            end
        end
        if has_log and has_lucy then
            self.woodie_human_lucy_3 = math.min(db.woodie_human_lucy_3,self.woodie_human_lucy_3 + 1)
        end
        if self.woodie_human_lucy_3 >= db.woodie_human_lucy_3 then
            self:_unlock('woodie_human_lucy_3')
        end
    end
end

---快速捡拾者
function woodie_skill_tree_newunlock_data:ForUnlock_QuickPick()
    self.woodie_human_quickpicker = math.min(db.woodie_human_quickpicker_3,self.woodie_human_quickpicker + 1)

    if self.woodie_human_quickpicker >= db.woodie_human_quickpicker_1 then
        self:_unlock('woodie_human_quickpicker_1')
    end
    if self.woodie_human_quickpicker >= db.woodie_human_quickpicker_2 then
        self:_unlock('woodie_human_quickpicker_2')
    end
    if self.woodie_human_quickpicker >= db.woodie_human_quickpicker_3 then
        self:_unlock('woodie_human_quickpicker_3')
    end
end

return woodie_skill_tree_newunlock_data